﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
#endregion


namespace BubbleSurvival
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class VideoOptionsMenuScreen : MenuScreen
    {
        #region Fields

        MenuEntry resolutionMenuEntry;
        MenuEntry fullScreenMenuEntry;
        MenuEntry setDefaultsMenuEntry;
        MenuEntry back;

        const int DEFRESOLUTIONINDEX = 1;

        static Vector2[] resolutionList = { new Vector2(1024, 768), new Vector2(1152, 720),
                                            new Vector2(1152, 768), new Vector2(1280, 720),
                                            new Vector2(1280, 800), new Vector2(1280, 1024)};

        static Vector2 fullScreenResolution;

        static int curResolutionIndex = DEFRESOLUTIONINDEX;

        public static Vector2 Resolution
        {
            get
            {
                if (fullScreen)
                {
                    return fullScreenResolution;
                }
                else
                {
                    return resolutionList[curResolutionIndex];
                }
            }
        }

        static bool niceEdges = true;
        public static bool NiceEdges
        {
            get { return niceEdges; }
        }
        public static string NiceEdgesAsString
        {
            get { return niceEdges ? "Circle" : "Simple"; }
        }

        static bool fullScreen = false;
        public static bool FullScreen
        {
            get { return fullScreen; }
        }

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public VideoOptionsMenuScreen()
            : base("Video Options")
        {


            // Create our menu entries.
            resolutionMenuEntry = new MenuEntry(string.Empty);
            fullScreenMenuEntry = new MenuEntry(string.Empty);

            SetMenuEntryText();

            back = new MenuEntry("Back");
            setDefaultsMenuEntry = new MenuEntry("Defaults");

            // Hook up menu event handlers.
            resolutionMenuEntry.SelectedRight += ResolutionMenuEntrySelectedRight;
            resolutionMenuEntry.SelectedLeft += ResolutionMenuEntrySelectedLeft;
            fullScreenMenuEntry.Selected += FullScreenMenuEntrySelected;
            setDefaultsMenuEntry.Selected += DefaultsMenuEntrySelected;
            back.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(resolutionMenuEntry);
            MenuEntries.Add(fullScreenMenuEntry);
            MenuEntries.Add(setDefaultsMenuEntry);
            MenuEntries.Add(back);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            resolutionMenuEntry.Text = "Resolution: " + (fullScreen ? "Fullscreen" : resolutionList[curResolutionIndex].ToString());
            fullScreenMenuEntry.Text = "Fullscreen: " + (fullScreen ? "yes" : "no");
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Resolution menu entry is selected.
        /// </summary>
        void ResolutionMenuEntrySelectedRight(object sender, EventArgs e)
        {
            if (!fullScreen)
            {
                curResolutionIndex++;
                if (curResolutionIndex == resolutionList.Length)
                    curResolutionIndex = 0;

                ScreenManager.changeResolution(resolutionList[curResolutionIndex]);

                SetMenuEntryText();
            }
        }

        void ResolutionMenuEntrySelectedLeft(object sender, EventArgs e)
        {
            if (!fullScreen)
            {
                curResolutionIndex--;
                if (curResolutionIndex < 0)
                    curResolutionIndex = resolutionList.Length - 1;

                ScreenManager.changeResolution(resolutionList[curResolutionIndex]);

                SetMenuEntryText();
            }
        }

        void FullScreenMenuEntrySelected(object sender, EventArgs e)
        {

            if (!fullScreen)
            {
                ScreenManager.changeToFullScreen();
                fullScreenResolution = ScreenManager.GetFullScreenResolution();
                fullScreen = true;
            }
            else
            {
                ScreenManager.changeToWindowed(resolutionList[curResolutionIndex]);
                fullScreen = false;
            }

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Defaults menu entry is selected.
        /// </summary>
        void DefaultsMenuEntrySelected(object sender, EventArgs e)
        {
            curResolutionIndex = DEFRESOLUTIONINDEX;
            niceEdges = true;
            ScreenManager.changeToWindowed(resolutionList[curResolutionIndex]);
            fullScreen = false;
            SetMenuEntryText();
        }


        #endregion
    }
}